#include<stdio.h>

#include "net.h"
#include "common.h"
#include "xml.h"

//global variables
global server;

int daemonize() 
{
    int fd;
	pid_t result;
	result = fork();
	if( result < 0 )
	{
		printf("fork() error,process exit...\n");
		exit(0);
	}
	else if( 0 == result )
	{
		/*child process*/
		setsid(); 
		if ( ( fd = open("/dev/null", O_RDWR, 0) ) != -1 ) 
		{
			dup2(fd, STDIN_FILENO);
			dup2(fd, STDOUT_FILENO);
			dup2(fd, STDERR_FILENO);
			if (fd > STDERR_FILENO) close(fd);
		}
	}
	else
	{
		/*parent process*/
		printf("Daemon, create child process ok,parent will exit...\n");
		exit(0);
	}    
}

void handleSignal()
{
	signal(SIGHUP, SIG_IGN);
    signal(SIGPIPE, SIG_IGN);
	signal(SIGSEGV, SIG_IGN);
	signal(SIGBUS, SIG_IGN);
	signal(SIGFPE, SIG_IGN);
	signal(SIGILL, SIG_IGN);	
}

void initServer()
{
	//set to 0 !
	bzero(&server,sizeof(global));
	epollfd = epoll_create(maxfd);
}

void main()
{
	//run as daemon process...
	daemonize();	
	//TODO ---adjust max open descriptors	
	handleSignal();	
	initServer();
	
	int fd = createServerSocket();
	if(OPEN_DCHAT_ERROR == fd )
	{
		exit(0);
	}
	//register server socket 
	server.fdDescription[fd].type = FD_SOCKET_LISTEN ;	
	
	//create epoll cycle now
	
	
	
	
	
	
	
	
}